<?php

/**
 * Implements a Heartbeat message flag.
 */
class flag_heartbeat_message extends flag_flag {
  function default_options() {
    $options = parent::default_options();
    $options += array(
      'show_on_heartbeat_message' => TRUE,
    );
    return $options;
  }

  function options_form(&$form) {
    parent::options_form($form);
    $form['types'] = array(
      // A user flag doesn't support node types. (Maybe will support roles instead, in the future.)
      '#type' => 'value',
      '#value' => array(0 => 0),
    );
    $form['display']['show_on_heartbeat_message'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link under heartbeat messages'),
      '#default_value' => $this->show_on_heartbeat_message,
      '#access' => empty($this->locked['show_on_heartbeat_message']),
    );
  }

  function _load_content($content_id) {
    return heartbeat_load_message_instance($content_id);
  }

  function applies_to_content_object($message_instance) {

    if ($message_instance) {
      return TRUE;
    }
    return FALSE;
  }

  function get_content_id($heartbeat_message) {
    return $heartbeat_message->uaid;
  }

  function uses_hook_link($teaser) {
    return $this->show_on_heartbeat_message;
  }

  function get_labels_token_types() {
    return array('comment', 'node', 'user','heartbeat_message');
  }

  function replace_tokens($label, $contexts, $content_id) {
    if ($content_id) {
      if (($message = $this->fetch_content($content_id))) {
        $contexts['heartbeat_message'] = $message;
      	if($message->uid > 0) {
      		$contexts['user'] = user_load($message->uid);
      	}
        if($message->nid > 0) {
          $contexts['node'] = node_load($message->nid);
        }
      }
    }
    return parent::replace_tokens($label, $contexts, $content_id);
  }

  function get_flag_action($content_id) {
    $flag_action = parent::get_flag_action($content_id);
    $heartbeat_message = $this->fetch_content($content_id);
    $flag_action->content_title = $heartbeat_message->message;
    return $flag_action;
  }

  function get_relevant_action_objects($content_id) {
    $heartbeat_message = $this->fetch_content($content_id);
    $objects = array('heartbeat_message' => $heartbeat_message);
    if($heartbeat_message->nid > 0) {
      $objects['node'] = node_load($heartbeat_message->nid);
    }
    return $objects;
  }

  function rules_get_event_arguments_definition() {
    return array(
      'heartbeat_message' => array(
        'type' => 'heartbeat_message',
        'label' => t('flagged heartbeat message'),
        'handler' => 'flag_rules_get_event_argument',
      ),
      'node' => array(
        'type' => 'node',
        'label' => t("the flagged heartbeat message's content if available"),
        'handler' => 'heartbeat_get_message_instance',
      ),
    );
  }

  function rules_get_element_argument_definition() {
    return array(
      'type' => 'heartbeat_message',
      'label' => t('Flagged heartbeat message')
    );
  }

  function applies_to_content_id_array($content_ids) {
    $passed = array();
    $content_ids  = implode(',', array_map('intval', $content_ids));
    $result = db_query("SELECT uaid FROM {hb_messages} hb WHERE uaid IN ($content_ids)");
    while ($row = db_fetch_object($result)) {
      $passed[$row->uaid] = TRUE;
    }
    return $passed;
  }
}